package com.itheima.controller;


import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController()
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 将查询到的部门集合传给前端
     * @return 返回查询到的部门数据集合
     */
    @GetMapping()
    public Result depts() {

        List<Dept> depts = deptService.querylist();
        return Result.success(depts);
    }

    /**
     * &#064;RequestParam  用来接收简单参数
     * 当接收参数名与
     * 删除部门
     * @param id  接受前端传过来的id值

     */
    @Log
    @DeleteMapping()
    public Result delDept( Integer id) {
        deptService.delete(id);
        return Result.success();
    }


    /**
     * 新增部门
     */
    @Log
    @PostMapping()
    public Result save(@RequestBody Dept dept) {

        deptService.save(dept);
        return Result.success();
    }

    /**
     * 查询回显
     *根据id查询部门
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {

        return Result.success(deptService.getDept(id));
    }

    /**
     * 修改部门
     */
    @Log
    @PutMapping()
    public Result update(@RequestBody Dept dept) {
        log.info("");
        deptService.update(dept);
        return Result.success();
    }
}
